package com.fastCms.business.bim.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fastCms.business.frame.constant.CommonConst;
import com.fastCms.business.frame.daos.BannerMapper;
import com.fastCms.business.frame.exception.BusinessException;
import com.fastCms.business.bim.pojo.po.BannerSavePO;
import com.fastCms.business.bim.pojo.po.ChangeStatusPO;
import com.fastCms.business.bim.pojo.po.ScenePO;
import com.fastCms.business.bim.pojo.vo.BannerVO;
import com.fastCms.business.frame.pojo.entity.Banner;
import com.fastCms.business.frame.pojo.po.IdStrPO;
import com.fastCms.business.frame.service.BannerService;
import com.fastCms.business.frame.utils.BeanConvertUtil;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author quan
 * @since 2024-05-23
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements BannerService {

    @Override
    public List<BannerVO> showList(ScenePO param) {
        String scene = param.getScene();
        LambdaQueryWrapper<Banner> bannerQueryWrapper = new LambdaQueryWrapper();
        bannerQueryWrapper.eq(Banner::getScene, scene);
        bannerQueryWrapper.eq(Banner::getOrganizeCode, param.getOrganizeCode());
        bannerQueryWrapper.eq(Banner::getState, CommonConst.STATE.VALID);
        List<Banner> list = list(bannerQueryWrapper);
        if (CollUtil.isEmpty(list)) {
            List<BannerVO> bannerVOList = BeanConvertUtil.convertList(list, BannerVO.class);
            return bannerVOList;
        }
        return Lists.newArrayList();
    }

    @Override
    public Boolean saveOrUpdate(BannerSavePO param) throws BusinessException {
        if (StringUtils.isBlank(param.getId())) {
            Banner banner = new Banner();
            BeanUtil.copyProperties(banner, param);
            this.save(banner);
        } else {
            Banner banner = getById(param.getId());
            if (banner == null) {
                throw new BusinessException("数据不存在");
            }
            BeanUtil.copyProperties(banner, param);
            this.updateById(banner);
        }
        return true;
    }

    @Override
    public Boolean delete(IdStrPO param) throws BusinessException {
        Banner banner = getById(param.getId());
        if (banner == null) {
            throw new BusinessException("数据不存在");
        }
        banner.setState(CommonConst.STATE.INVALID);
        this.updateById(banner);
        return true;
    }

    @Override
    public Boolean changeStatus(ChangeStatusPO param) throws BusinessException {
        Banner banner = getById(param.getId());
        if (banner == null) {
            throw new BusinessException("数据不存在");
        }
        banner.setStatus(param.getStatus());
        this.updateById(banner);
        return true;
    }
}
